<?php
function getTree($data)
{
    $html = '';
    foreach($data as $k => $v)
    {
        if(isset($v['cate_child']))
        {   //父亲找到儿子
            $html .= '<li><a tname="cate_ids" tvalue="'.$v['cate_id'].'">'.$v['cate_name'].'</a>';
            $html .= getTree($v['cate_child']);
            $html .= '</li>';
        }else{
            $html .= '<li><a tname="cate_ids" tvalue="'.$v['cate_id'].'">'.$v['cate_name'].'</a></li>';
        }
    }
    return '<ul>'.$html.'</ul>';
}

$parent_array = array();//记录每一个父节点的地址引用
$tree_array = array();//整颗树的数组

foreach($menu_arr as $v){

    $node_num = ($v['right_value']-$v['left_value'] - 1)/2;

    if($v['parent_cate_id']){//如果没有parent_cate_id则是顶层

        $parent_array[$v['parent_cate_id']]['cate_child'][$v['cate_id']] = $v;


        if($node_num > 0 || ($node_num == 0 && $v['action'] == '')){

            $parent_array[$v['cate_id']] = &$parent_array[$v['parent_cate_id']]['cate_child'][$v['cate_id']];
            $parent_array[$v['cate_id']]['cate_child'] = array();
        }

    }else{
        $tree_array[$v['cate_id']] = $v;

        if($node_num > 0 || ($node_num == 0 && $v['action'] == '')){

            $parent_array[$v['cate_id']] = &$tree_array[$v['cate_id']];
            $parent_array[$v['cate_id']]['cate_child'] = array();
        }
    }
}
// p($tree_array);
?>
<div class="pageContent">
    <div class="pageFormContent" layoutH="60">
        <ul class="tree treeFolder expand" id="_select_cate">
            <li><a tname="cate_ids" tvalue="0">管理中心</a>
                <?php echo getTree($tree_array);?>
            </li>
        </ul>
    </div>
    <div class="formBar">
        <ul>
            <li><a class="button"  href="javascript:void(0);" onclick="cateTreeCallBack();"><span>确认</span></a></li>
            <li><div class="button"><div class="buttonContent"><button class="close" type="button">关闭</button></div></div></li>
        </ul>
    </div>
</div>
<script type="text/javascript">
    var olda = '';

    (function($){
        $('#_select_cate').find('a').click(function(){
            if(olda){
                olda.text(olda.text().replace('(√)',''));
            }

            $(this).text($(this).text()+'(√)');
            olda = $(this);
        });

        var pdialog = $("body").data('add_cate');
        var sids = $('input[name="parent_cate_id"]', pdialog).val();

        setTimeout(function(){//这里代码有延迟事件，所以用setTimeout调整执行顺序
            var dialog = $("body").data('cate_tree');
            var el = dialog.find("a[tvalue='"+sids+"']");

            el.text(el.text()+'(√)');
            olda = el;
        },0);

    })(jQuery);

    function cateTreeCallBack(){
        if(olda){

            var pdialog = $("body").data('add_cate');
            $('input[name="parent_cate_id"]', pdialog).val(olda.attr('tvalue'));
            $('input[name="parent_cate_name"]', pdialog).val(olda.text().replace('(√)',''));
            $.pdialog.closeCurrent();
        }else{
            alertMsg.error('必须选择一个分类！');
        }
    }
</script>